﻿using JESAI.Application.Abstractions.Test;
using JESAI.Application.Abstractions.Test.Dtos;
using JESAI.AutoWebApi.Setting;
using JESAI.Core.Aspects;
using JESAI.Core.Attributes;
using JESAI.DDD.Services;
using JESAI.DynamicCodeRun.Providers;
using JESAI.DynamicCodeRun.Run;
using JESAI.Platform.Domain.Abstractions;
using JESAI.Platform.Domain.Admin.Repositories;
using JESAI.Platform.Domain.Test.Manager;
using JESAI.Uow;
using Newtonsoft.Json;
using Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Test
{
    [AppService]
    public class TestApplicationService : ApplicationService, IAutoApiService, ITestApplicationService
    {
        private readonly ITestManager _testManager;
        private readonly IDynamicCodeRunProvider _dynamicCodeRunProvider;

        private IUnitOfWorkManager _unitOfWorkManager { get; set; }

        public TestApplicationService(ITestManager testManager, IUnitOfWorkManager unitOfWorkManager, IDynamicCodeRunProvider dynamicCodeRunProvider)
        {
            _testManager = testManager;
            _unitOfWorkManager = unitOfWorkManager;
            _dynamicCodeRunProvider = dynamicCodeRunProvider;

        }
        public List<TestDto> GetAllTest()
        {
            return _testManager.GetAll();
        }

        public async Task<string> TestCodeRun()
        {
           
            string code = @"using Newtonsoft.Json;using System;using JESAI.Core.Aspects;using JESAI.Platform.Domain.Admin.Repositories;public class MyClass{public string Handle(string param){  var dictRepository = ServiceLocator.Instance.GetService<IDictRepository>();
            var dict = dictRepository.GetList();
            var str=JsonConvert.SerializeObject(dict);return str;}}";
            //var result = CodeRun.Run("111111", code, "test", languageVersion: 2147483647, "");
            var result = await _dynamicCodeRunProvider.RunCode("111111", code, "test", languageVersion: 2147483647, "");
            return result.Output;
        }
    }
}
